<?php
$name = $_GET['name'];
$jjj = $_GET['jj'];
$gs = $_GET['gs'];
$img = $_GET['img'];
$jsonFilePath = '../json/template.json';
$jsonContent = file_get_contents($jsonFilePath);
$data = json_decode($jsonContent, true);
if (is_null($data)) {
    echo "Error decoding JSON";
    exit;
}
$highestKey = 0;
foreach ($data as $key => $value) {
    if (is_numeric($key) && intval($key) > $highestKey) {
        $highestKey = intval($key);
    }
}
require('../../../cofd/common.php');
$newKey = $highestKey + 1;
$sql = "select exmattm,exmatanse from `exam_page` where exmatid='$gs'";
$res = mysqli_query($conn, $sql);
$row = mysqli_fetch_array($res);
$mat = base64_encode($row['exmattm']);
$mat2 = base64_encode($row['exmatanse']);
$conn->close();
$newEntry = [
    "name" => "$name",
    "img" => "$img",
    "say" => "$jjj",
    "content" => "$mat",
    "answer" => "$mat2"
];
$data[$newKey] = $newEntry;
$newJsonContent = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
file_put_contents($jsonFilePath, $newJsonContent);
echo json_encode(array("code" => 1));
?>